package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.SfcRate;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface SfcRateDao {

    @Results(id = "sfcRateDaoResultMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "code", property = "code", jdbcType = JdbcType.VARCHAR),
            @Result(column = "rate", property = "rate", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "last_in_station_time", property = "lastInstationTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "denominator", property = "denominator", jdbcType = JdbcType.INTEGER),
            @Result(column = "molecule", property = "molecule", jdbcType = JdbcType.INTEGER),
            @Result(column = "serial_num", property = "serialNum", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
    })

    /*
     *查询单个
     */
    @Select("<script> " +
            " select * " +
            " from " + DB2TableName.SFC_Rate +
            " where code = #{code}" +
            " order by sort desc" +
            " limit 1 " +
            "</script>")
    SfcRate getByCode(@Param("code") String code);

    @Insert("insert into " +  DB2TableName.SFC_Rate + "(code, rate, create_time, last_in_station_time, denominator, molecule, serial_num, sort) " +
            "values (#{code}, #{rate}, #{createTime}, #{lastInstationTime}, #{denominator}, #{molecule}, #{serialNum}, #{sort})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insert(SfcRate param);

    @Update("UPDATE " + DB2TableName.SFC_Rate + " SET code=#{code},rate=#{rate}," +
            "last_in_station_time=#{lastInstationTime}," +
            "denominator=#{denominator},molecule=#{molecule}," +
            "serial_num=#{serialNum} " +
            "WHERE id=#{id}")
    Integer update(SfcRate param);

    @Delete("DELETE FROM " + DB2TableName.SFC_Rate +
            " WHERE code = #{code} AND sort = #{sort}")
    Integer deleteOldRateBySort(@Param("code") String code, @Param("qty") Integer sort);

    @Delete("delete from " + DB2TableName.SFC_Rate + " where id = #{id}")
    Integer deleteById(@Param("id") Integer id);

    @Select("SELECT MAX(sort) FROM " + DB2TableName.SFC_Rate + " WHERE code = #{code}")
    Integer getMaxSortByCode(@Param("code") String code);

    @Select("SELECT * FROM sfc_rate WHERE code = #{code} ORDER BY sort DESC LIMIT 1")
    SfcRate getLatestByCode(@Param("code") String code);
}
